//login.js
//获取应用实例
const app = getApp()
var util =  require('../../utils/util.js');
Page({
  
  data: {
    array: ["男", "女"],
    index: '',
    sex:'',
    date:'',
    stature: '',
    downImg: '/resources/down.png',
    upImg: '/resources/up.png',
    btnTxt:{
      btnTxt:'进入'
    },
    userInfo: {},
    isHeight:false,
    styles: {
      line: '#d0d0d0',
      bginner: '#fbfbfb',
      bgoutside: '#ffffff',
      lineSelect: '#2897ff',
      font: '#000000'
    }
  },
  //picker
  bindSexChange: function (e) {
    this.setData({
      index: e.detail.value,
      sex: this.data.array[e.detail.value],
      sexImgsrc: this.data.downImg,
    })
  },

  bindSexCancel:function(){
    this.setData({
      sexImgsrc: this.data.downImg
    })
  },

  bindDateChange: function(e) {
    this.setData({
      date: e.detail.value,
      dateImgsrc: this.data.downImg
    })
  },

  bindDateCancel:function(){
    this.setData({
      dateImgsrc: this.data.downImg
    })
  },
  
  //选中的身高
  bindvalue: function (e) {
    this.setData({
      value: e.detail.value,
    })
  },

  //选择箭头
  imgChange:function(e)
  {
    var that = this
    if(e.currentTarget.dataset.type == "性别")
    {
      if(that.data.sexImgsrc == that.data.downImg)
      {
        that.setData({
          sexImgsrc: that.data.upImg
        })
      }
    }
    else if(e.currentTarget.dataset.type == "日期"){
      if(that.data.dateImgsrc == that.data.downImg)
      {
        that.setData({
          dateImgsrc: that.data.upImg
        })
      }
    }
  },

  onLoad: function () {
    // console.log(app.globalData.userInfo)
    if (app.globalData.userInfo) {
      this.setData({
        userInfo: app.globalData.userInfo,
        heightImgsrc: this.data.downImg,
        sexImgsrc: this.data.downImg,
        dateImgsrc: this.data.downImg,
      })
    } 
    else {
      // 在没有 open-type=getUserInfo 版本的兼容处理
      wx.getUserInfo({
        success: res => {
          app.globalData.userInfo = res.userInfo
          this.setData({
            userInfo: res.userInfo
          })
        }
      })
    }
  },

  //选择身高
  selectHeight: function (e) {
    var currentStatu = e.currentTarget.dataset.statu;
    this.anim(currentStatu)
  },

  //动画部分
  anim: function (currentStatu) {
    // 第1步：创建动画实例 
    var animation = wx.createAnimation({
      duration: 200,  //动画时长
      timingFunction: "linear", //线性
      delay: 0  //0则不延迟
    });
    
    // 第2步：这个动画实例赋给当前的动画实例
    this.animation = animation;
 
    // 第3步：执行第一组动画：Y轴偏移240px后(盒子高度是240px)，停
    animation.translateY(280).step();
 
    // 第4步：导出动画对象赋给数据对象储存
    this.setData({
      animationData: animation.export()
    })
    
    // 第5步：设置定时器到指定时候后，执行第二组动画
    setTimeout(function () {
      // 执行第二组动画：Y轴不偏移，停
      animation.translateY(0).step()
      // 给数据对象储存的第一组动画，更替为执行完第二组动画的动画对象
      this.setData({
        animationData: animation
      })
      
      //完成
      if (currentStatu == "complete") {
        this.setData(
          {
            isHeight: false,
            stature: this.data.value,
            heightImgsrc: this.data.downImg
          }
        );
      }

      //取消
      else if (currentStatu == "cancel") {
        this.setData(
          {
            isHeight: false,
            heightImgsrc: this.data.downImg
          }
        );
      }
    }.bind(this), 200)
  
    // 显示抽屉
    if (currentStatu == "open") {
      this.setData(
        {
          isHeight: true,
          heightImgsrc: this.data.upImg
        }
      );
    }
  },

  //阻止穿透
  preventTouchMove: function () {
    // console.log('stop user scroll it!');
    return;
  },
  
  lickBtn: function () {
    var that = this
    if(that.data.sex == ''){
      util.showModal('提示','请选择性别')
      return;
    }
    if(that.data.stature == '')
    {
      util.showModal('提示','请选择身高')
      return;
    }
    if(that.data.date == '')
    {
      util.showModal('提示','请选择出生日期')
      return;
    }
    that.setData({
      btnDis:true
    })
    let nickName = ''
    if(util.isEmojiCharacter(app.globalData.userInfo.nickName.trim()))
    {
      nickName = '--'
    }
    else{
      nickName = app.globalData.userInfo.nickName
    }
    // console.log(nickName)
    wx.request({
      url: app.globalData.apiUrl + '/add_user',
      data: {
        nickname: nickName,
        openid: app.globalData.openid,
        headimgurl: app.globalData.userInfo.avatarUrl,
        sex: that.data.sex,
        birthday: that.data.date,
        height: that.data.stature,
      },
      method: 'post',
      header: {
        'content-type': 'application/x-www-form-urlencoded' //默认值
      },
      success: function (res) {
        console.log(res)
        app.globalData.userId = res.data.userid;
        wx.switchTab({
          url: '../index/index',
        })
      },
      fail: function (res) {
        wx.showModal({
          title: '提示',
          content: '进入失败，请重试进入',
          showCancel: false,
          complete (res) {
            that.setData({
              btnDis: false
            })
          }
        })
      }
    })
  },
  
})